package helper.request.plan;

import java.sql.SQLException;
import java.util.ArrayList;

import server.dbconnector.DatabaseConnector;

import helper.request.*;
import base.Plan;
import base.User;

public class ModifyPlanRequest implements PlanRequest,RequestUndo{
	private static final long serialVersionUID = -8303109114785733114L;
	private User currentuser;
	private Plan currentPlan;
	private Plan cloneCurrentPlan;
	private Plan preplan;
	private ArrayList<Plan> planList;
	
	public ModifyPlanRequest(User currentuser,Plan preplan,Plan currentPlan,ArrayList<Plan> planList){
		this.currentuser=currentuser;
		this.preplan=preplan;
		this.currentPlan=currentPlan;
		this.planList=planList;
	}
	@Override
	public Plan getPlan() {
		return cloneCurrentPlan;
	}

	@Override
	public User getcurrentuser() {
		return currentuser;
	}

	@Override
	public int getcurrentuserid() {
		return currentuser.getid();
	}
	@Override
	public boolean execute() {
		try{
			planList.remove(preplan);
			planList.add(currentPlan);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean de_execute() {
		try{
			planList.remove(currentPlan);
			planList.add(preplan);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public void process(DatabaseConnector dc)throws SQLException {
		String availstr="";
		if(currentPlan.getavailable()!=null)
		for(int i=0;i<this.currentPlan.getavailable().size();i++){
			availstr+=this.currentPlan.getavailable().get(i).toString();
			if(i!=this.currentPlan.getavailable().size()-1)availstr+=",";
		}
		System.err.println(availstr);
		dc.commit("update plan set title='"+this.currentPlan.gettitle()+"' where ID="+this.currentPlan.getid());
		dc.commit("update plan set content='"+this.currentPlan.getcontent()+"' where ID="+this.currentPlan.getid());
		dc.commit("update plan set level="+this.currentPlan.getlevel()+" where ID="+this.currentPlan.getid());
		dc.commit("update plan set state="+this.currentPlan.getState()+" where ID="+this.currentPlan.getid());
		dc.commit("update plan set visible="+this.currentPlan.getVisible()+" where ID="+this.currentPlan.getid());
		dc.commit("update plan set availableto='"+availstr+"' where ID="+this.currentPlan.getid());
	}
	public String toString(){
		return "修改了原计划《"+preplan.gettitle()+"》";
	}

}
